﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using BMS.Entity;
using System.Data;

namespace BMS.ConvertToEntity
{
    public class Transaction
    {
        public string Id = string.Empty;
        
        public Transaction()
        {
            Id = Guid.NewGuid().ToString();
        }

        public Functionary GetFunctionaryFromDataTable(DataTable functionary)
        {
            return new Functionary()
            {
                IdInBiometricDevice = functionary.Select().Count() > 0 ? functionary.Rows[0]["ID_BIOMETRIC"].ToString() : string.Empty,
                DocumentNumber = functionary.Select().Count() > 0 ? functionary.Rows[0]["DOCUMENT_NUMBER"].ToString() : string.Empty,
                Name = functionary.Select().Count() > 0 ? functionary.Rows[0]["NAME"].ToString() : string.Empty,
                Company = functionary.Select().Count() > 0 ? functionary.Rows[0]["COMPANY"].ToString() : string.Empty,
                Headquarter = functionary.Select().Count() > 0 ? functionary.Rows[0]["HEADQUARTER"].ToString() : string.Empty,
                Charge = functionary.Select().Count() > 0 ? functionary.Rows[0]["CHARGE_CODE"].ToString() : string.Empty,
                CostCenter = string.Empty
            };
        }

        public TurnAccess GetTurnAccessFromDataTable(DataTable turnAccess)
        {
            return new TurnAccess()
            {
                Id = this.Id,
                Turn = turnAccess.Select().Count() > 0 ? turnAccess.Rows[0]["TURN"].ToString() : string.Empty,
                TurnCode = turnAccess.Select().Count() > 0 ? turnAccess.Rows[0]["TURN_CODE"].ToString() : string.Empty,
                ActualDate = turnAccess.Select().Count() > 0 ? Convert.ToDateTime(turnAccess.Rows[0]["ACTUAL_DATE"].ToString()) : new DateTime(),
                TurnDate = turnAccess.Select().Count() > 0 ? Convert.ToDateTime(turnAccess.Rows[0]["TURN_DATE"].ToString()) : new DateTime(),
                TurnTotalHours = turnAccess.Select().Count() > 0 ? Convert.ToDateTime(turnAccess.Rows[0]["TOTAL_HOURS"].ToString()).Hour : 0,
                TurnTotalMinutes = turnAccess.Select().Count() > 0 ? Convert.ToDateTime(turnAccess.Rows[0]["TOTAL_HOURS"].ToString()).Minute : 0,
                MinutesBeforeEntry = turnAccess.Select().Count() > 0 ? int.Parse(turnAccess.Rows[0]["MINUTES_BEFORE_ENTRY"].ToString()): 0,
                MinutesAfterEntry = turnAccess.Select().Count() > 0 ? int.Parse(turnAccess.Rows[0]["MINUTES_AFTER_ENTRY"].ToString()) : 0,
                CanAlwaysEntry = turnAccess.Select().Count() > 0 ? bool.Parse(turnAccess.Rows[0]["CAN_ALWAYS_ENTRY"].ToString()) : false
            };
        }

        public BiometricDevice GetBiometricDevice(DataTable biometricDevice)
        {
            return new BiometricDevice();
        }
    }
}
